******************************************************************************************************************
*		
*		Tile: Poll Watchers, Polling Stations, and Electoral Manipulation
*
*		Authors: Sergio Ascencio and Miguel R. Rueda
* 
*		Description: estimates two_step_model with census data
*
*		Modified: 03/10/15 by M.
*
******************************************************************************************************************


cd "C:\Users\mrueda\Documents\Emory\Papers\Party_Delegates\data\Controles\"

use aux_matlab_census.dta,clear
drop prPRI1-prPAN3
xtset id_section year2, generic

tab d_RC_PRI_c, gen(prPRI)
global yPRI1 "prPRI1"
global yPRI2 "prPRI2"
global yPRI3 "prPRI3"

tab d_RC_PAN_c, gen(prPAN)
global yPAN1 "prPAN1"
global yPAN2 "prPAN2"
global yPAN3 "prPAN3"


*Types unrestricted

capture program drop lfmsec_ur
program lfmsec_ur
  version 15.1
  *numbers denote actions and letters denote players
  args lnf xb2PRI xb3PRI xb2PAN xb3PAN 
  tempvar p1PRI p2PRI p3PRI p1PAN p2PAN p3PAN 
  quietly {
     
	 gen double `p1PRI' = 1/(1+exp(`xb2PRI')+exp(`xb3PRI'))
     gen double `p2PRI' = exp(`xb2PRI')/(1+exp(`xb2PRI')+exp(`xb3PRI'))
     gen double `p3PRI' = exp(`xb3PRI')/(1+exp(`xb2PRI')+exp(`xb3PRI'))
     
	 gen double `p1PAN' = 1/(1+exp(`xb2PAN')+exp(`xb3PAN'))
     gen double `p2PAN' = exp(`xb2PAN')/(1+exp(`xb2PAN')+exp(`xb3PAN'))
     gen double `p3PAN' = exp(`xb3PAN')/(1+exp(`xb2PAN')+exp(`xb3PAN'))
     
	 replace `lnf' = $yPRI1*ln(`p1PRI') + $yPRI2*ln(`p2PRI') + $yPRI3*ln(`p3PRI') + $yPAN1*ln(`p1PAN') + $yPAN2*ln(`p2PAN') + $yPAN3*ln(`p3PAN')
   }
end


 capture program drop game2stage
 program game2stage, eclass
	syntax [if]
	marksample touse
	tempname b b1 dPRIm_m dPRIh_m dPANm_m dPANh_m dPRIm_h dPRIh_h dPANm_h dPANh_h
	tempvar pPRI_M pPRI_H pPAN_M pPAN_H
	
	quietly {
		mlogit d_RC_PRI_c l_d_RC_others l_margin lcasillas school_ic l_diff_PRI_PAN lpop l_turnout local ldist_city ilit_s basic_amen linsurance ///
			l_d_RC_others_sq l_margin_sq lcasillas_sq school_ic_sq l_diff_PRI_PAN_sq lpop_sq l_turnout_sq ldist_city_sq ilit_s_sq basic_amen_sq linsurance_sq ///
			l_d_RC_others_l_margin l_d_RC_others_lcasillas l_d_RC_others_school_ic l_d_RC_others_l_diff_PRI_PAN l_d_RC_others_lpop l_d_RC_others_l_turnout l_d_RC_others_local l_d_RC_others_ldist_city l_d_RC_others_ilit_s l_d_RC_others_basic_amen l_d_RC_others_linsurance ///
			l_margin_lcasillas l_margin_school_ic l_margin_l_diff_PRI_PAN l_margin_lpop l_margin_l_turnout l_margin_local l_margin_ldist_city l_margin_ilit_s l_margin_basic_amen l_margin_linsurance ///
			lcasillas_school_ic lcasillas_l_diff_PRI_PAN lcasillas_lpop lcasillas_l_turnout lcasillas_local lcasillas_ldist_city lcasillas_ilit_s lcasillas_basic_amen lcasillas_linsurance ///
			school_ic_l_diff_PRI_PAN school_ic_lpop school_ic_l_turnout school_ic_local school_ic_ldist_city  school_ic_ilit_s school_ic_basic_amen school_ic_linsurance ///
			l_diff_PRI_PAN_lpop l_diff_PRI_PAN_l_turnout l_diff_PRI_PAN_local l_diff_PRI_PAN_ldist_city l_diff_PRI_PAN_ilit_s l_diff_PRI_PAN_basic_amen l_diff_PRI_PAN_linsurance ///
			lpop_l_turnout lpop_local lpop_ldist_city lpop_ilit_s lpop_basic_amen lpop_linsurance ///
			l_turnout_local l_turnout_ldist_city l_turnout_ilit_s l_turnout_basic_amen l_turnout_linsurance  ///
			local_ldist_city  local_ilit_s local_basic_amen local_linsurance ///
			ilit_s_basic_amen ilit_s_linsurance ///
			basic_amen_linsurance ///
			ldist_pri gov_PRI if year>=2009, baseoutcome(0) vce (cluster id_section) nopv

			predict `pPRI_M',outcome(1)
			predict `pPRI_H',outcome(2)

			
		mlogit d_RC_PAN_c l_d_RC_others l_margin lcasillas school_ic l_diff_PRI_PAN lpop l_turnout local ldist_city ilit_s basic_amen linsurance ///
			l_d_RC_others_sq l_margin_sq lcasillas_sq school_ic_sq l_diff_PRI_PAN_sq lpop_sq l_turnout_sq ldist_city_sq ilit_s_sq basic_amen_sq linsurance_sq ///
			l_d_RC_others_l_margin l_d_RC_others_lcasillas l_d_RC_others_school_ic l_d_RC_others_l_diff_PRI_PAN l_d_RC_others_lpop l_d_RC_others_l_turnout l_d_RC_others_local l_d_RC_others_ldist_city l_d_RC_others_ilit_s l_d_RC_others_basic_amen l_d_RC_others_linsurance ///
			l_margin_lcasillas l_margin_school_ic l_margin_l_diff_PRI_PAN l_margin_lpop l_margin_l_turnout l_margin_local l_margin_ldist_city l_margin_ilit_s l_margin_basic_amen l_margin_linsurance ///
			lcasillas_school_ic lcasillas_l_diff_PRI_PAN lcasillas_lpop lcasillas_l_turnout lcasillas_local lcasillas_ldist_city lcasillas_ilit_s lcasillas_basic_amen lcasillas_linsurance ///
			school_ic_l_diff_PRI_PAN school_ic_lpop school_ic_l_turnout school_ic_local school_ic_ldist_city  school_ic_ilit_s school_ic_basic_amen school_ic_linsurance ///
			l_diff_PRI_PAN_lpop l_diff_PRI_PAN_l_turnout l_diff_PRI_PAN_local l_diff_PRI_PAN_ldist_city l_diff_PRI_PAN_ilit_s l_diff_PRI_PAN_basic_amen l_diff_PRI_PAN_linsurance ///
			lpop_l_turnout lpop_local lpop_ldist_city lpop_ilit_s lpop_basic_amen lpop_linsurance ///
			l_turnout_local l_turnout_ldist_city l_turnout_ilit_s l_turnout_basic_amen l_turnout_linsurance  ///
			local_ldist_city  local_ilit_s local_basic_amen local_linsurance ///
			ilit_s_basic_amen ilit_s_linsurance ///
			basic_amen_linsurance ///
			ldist_pan gov_PAN if year>=2009, baseoutcome(0) vce (cluster id_section) nopv

			predict `pPAN_M',outcome(1)
			predict `pPAN_H',outcome(2)

	}
	
	gen pPRIm=`pPRI_M'
	gen pPRIh=`pPRI_H'
	gen pPANm=`pPAN_M'
	gen pPANh=`pPAN_H'
	
	
	ml model lf lfmsec_ur (eq1: l_d_RC_others l_margin lcasillas school_ic l_diff_PRI_PAN lpop l_turnout local ldist_city ilit_s basic_amen linsurance ///
						ldist_pri gov_PRI pPANm pPANh) ///
						 (eq2: l_d_RC_others l_margin lcasillas school_ic l_diff_PRI_PAN lpop l_turnout local ldist_city ilit_s basic_amen linsurance ///
						ldist_pri gov_PRI pPANm pPANh) ///
						 (eq3: l_d_RC_others l_margin lcasillas school_ic l_diff_PRI_PAN lpop l_turnout local ldist_city ilit_s basic_amen linsurance ///
						ldist_pan gov_PAN pPRIm pPRIh) ///
						 (eq4: l_d_RC_others l_margin lcasillas school_ic l_diff_PRI_PAN lpop l_turnout local ldist_city ilit_s basic_amen linsurance ///
						ldist_pan gov_PAN pPRIm pPRIh)
	capture noisily ml maximize, diff 
	
	display "maximize" _rc
	if _rc~=0 {
		matrix `b'=J(1,68,0)
		matrix `dPRIm_m'=J(1,13,0)
		matrix `dPRIh_m'=J(1,13,0)
		matrix `dPANm_m'=J(1,13,0)
		matrix `dPANh_m'=J(1,13,0)
		matrix `dPRIm_h'=J(1,13,0)
		matrix `dPRIh_h'=J(1,13,0)
		matrix `dPANm_h'=J(1,13,0)
		matrix `dPANh_h'=J(1,13,0)
		}
	else{
		matrix `b1'=e(b)

		quietly{
			margins , at(pPANm=(0.1(0.025)0.4) pPANh=.6 gov_PRI=0 local=1) atmeans expression(exp(predict(equation(eq1)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
			matrix `dPRIm_m'=r(b)
			margins , at(pPANm=(0.1(0.025)0.4) pPANh=.6 gov_PRI=0 local=1) atmeans expression(exp(predict(equation(eq2)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
			matrix `dPRIh_m'=r(b)
			
			margins , at(pPRIm=(0.1(0.025)0.4) pPRIh=.6 gov_PAN=0 local=1) atmeans expression(exp(predict(equation(eq3)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
			matrix `dPANm_m'=r(b)
			margins , at(pPRIm=(0.1(0.025)0.4) pPRIh=.6 gov_PAN=0 local=1) atmeans expression(exp(predict(equation(eq4)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
			matrix `dPANh_m'=r(b)
			
			margins , at(pPANh=(0.2(0.05)0.8) gov_PRI=0 local=1) atmeans expression(exp(predict(equation(eq1)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
			matrix `dPRIm_h'=r(b)
			margins , at(pPANh=(0.2(0.05)0.8) gov_PRI=0 local=1) atmeans expression(exp(predict(equation(eq2)))/(1+exp(predict(equation(eq1)))+exp(predict(equation(eq2))))) nose
			matrix `dPRIh_h'=r(b)
			
			margins , at(pPRIh=(0.2(0.05)0.8) gov_PAN=0 local=1) atmeans expression(exp(predict(equation(eq3)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
			matrix `dPANm_h'=r(b)
			margins , at(pPRIh=(0.2(0.05)0.8) gov_PAN=0 local=1) atmeans expression(exp(predict(equation(eq4)))/(1+exp(predict(equation(eq3)))+exp(predict(equation(eq4))))) nose
			matrix `dPANh_h'=r(b)
			}
	}		
	matrix `b'=(`b1',`dPRIm_m',`dPRIh_m',`dPANm_m',`dPANh_m',`dPRIm_h',`dPRIh_h',`dPANm_h',`dPANh_h')		
		
	drop pPRIm pPRIh pPANm pPANh
	ereturn post `b' ,esample(`touse')		
	
end


keep if year>=2009

game2stage
matrix b=e(b)
matrix res_boot=b
set seed 1313

set matsize 800

forvalues i = 1/500 {
	preserve
	bsample ,cluster(state_district)
	game2stage
	matrix b=e(b)
	matrix res_boot=(res_boot\b)
	display `i'
	restore
}

svmat double res_boot
keep res_boot1-res_boot172

forvalue i=69/124 {
	replace res_boot`i'=. if res_boot`i'==0
}	

tabstat res_boot1-res_boot124 if _n~=1,s(sd) save
matrix sd_mat=r(StatTotal)
keep if _n==1
gen dum=1
reshape long res_boot, i(dum) j(estimates)
drop dum estimates
rename res_boot estimates
matrix sd_mat=sd_mat'
svmat sd_mat

*Generating pvals and confidence intervals
gen pval=min(2*normal(estimates/sd_mat),2*(1-normal(estimates/sd_mat)))
gen lci=estimate+invnormal(0.025)*sd_mat
gen uci=estimate+invnormal(0.975)*sd_mat


*Table 11*
keep if _n==1|_n==10|_n==11|_n==12|_n==15|_n==16|_n==18|_n==27|_n==28|_n==29|_n==32|_n==33|_n==35|_n==44|_n==45|_n==46|_n==49|_n==50|_n==52|_n==61|_n==62|_n==63|_n==66|_n==67 
